package com.taosdata.jdbc.springbootdemo.controller;

import com.taosdata.jdbc.springbootdemo.domain.Weather;
import com.taosdata.jdbc.springbootdemo.domain.YakenMqtt;
import com.taosdata.jdbc.springbootdemo.service.WeatherService;
import com.taosdata.jdbc.springbootdemo.service.YakenMqttService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;

@RequestMapping("/yaken")
@RestController
public class YakenMqttController {

	@Autowired
	private YakenMqttService yakenMqttService;

	/**
	 * create database and table
	 *
	 * @return
	 */
	@GetMapping("/init")
	public boolean init() {
		return yakenMqttService.init();
	}

	/**
	 * Pagination Query
	 *
	 * @param limit
	 * @param offset
	 * @return
	 */
//    @GetMapping("/{limit}/{offset}")
//    public List<Weather> queryWeather(@PathVariable Long limit, @PathVariable Long offset) {
//        return weatherService.query(limit, offset);
//    }

	/**
	 * upload single weather info
	 *
	 * @param temperature
	 * @param humidity
	 * @return
	 */
//    @PostMapping("/{temperature}/{humidity}")
//    public int saveWeather(@PathVariable int temperature, @PathVariable float humidity) {
//
//        return weatherService.save(temperature, humidity);
//    }

	/**
	 * upload multi weather info
	 *
	 * @param weatherList
	 * @return
	 */
	@GetMapping("/batch/{bs}/{m}")
	public int batchSaveWeather(@PathVariable("bs") Integer bs, @PathVariable("m") Integer m) {
		List<YakenMqtt> weatherList = new ArrayList<>();
		YakenMqtt mqtt = null;
//		int tot = bs;
		for (int i = 0; i < bs; i++) {
			mqtt = new YakenMqtt();
			mqtt.setD80(random9());
			mqtt.setD82(random9());
			mqtt.setD84(random100());
			mqtt.setD86(random100());
			mqtt.setD90(random100());

			mqtt.setD320(random9());
			mqtt.setD322(random9());
			mqtt.setD324(random9());
			mqtt.setD326(random9());
			mqtt.setD328(random100());
			mqtt.setD330(random100());
			mqtt.setD332(random100());
			mqtt.setD334(random100());
			weatherList.add(mqtt);
			if ((i % m == 0) || i == m) {
				yakenMqttService.save(weatherList);
//				tot = tot - h;
//				System.out.println("----------"+ Thread.currentThread().getName()+"----------------" + tot);
				weatherList = new ArrayList<>();
			}
		}
		if (!CollectionUtils.isEmpty(weatherList)) {
			yakenMqttService.save(weatherList);
//			tot = tot - h;
//			System.out.println("----------"+ Thread.currentThread().getName()+"----------------" + tot);
		}
		return 1;
	}

	public static int random100() {
		return (int) (100 * Math.random());
	}

	public static float random9() {
		return (float) (9 + Math.random());
	}

	public static void main(String[] args) {
		for (int i = 0; i < 100; i++) {
			System.out.println(random9());
		}
	}
}
